/**
 * Created by Eric on 14-1-13.
 */
define(function (require, exports, module) {

        var $ = require('./jquery'), _DC = require('lib/dyc/DynamicComponent');
        require('lib/plugins/jquery.element')($);
        require('lib/dyc/WrapHtml')(_DC, $);
        require('lib/dyc/LazyLoadComponent')(_DC, $);
        require('lib/dyc/SubCompont')(_DC, $);
        require('lib/dyc/Widgets')(_DC, $);
        require('lib/dyc/ActionCallBack')(_DC, $);
        _DC.depend(exports);

        exports.arrayMerge = function () {
            var newArray = [];
            if (typeof arguments.length == "undefined") {
                return newArray;
            }

            for (var i = 0; i < arguments.length; i++) {
                var arr = arguments[i];
                var attrArray = [];
                if ($.isArray(arr)) {
                    attrArray = arr;
                } else {
                    attrArray[0] = arr;

                }
                exports.merge2array(attrArray, newArray);
            }
            return newArray;
        };

        exports.merge2array = function (src, target) {
            if (!$.isArray(target)) {
                return false;
            }

            if ($.isArray(src)) {
                $.each(src, function (i, data) {
                    if ($.isArray(data)) {
                        merge2array(data, target);
                    }
                    target.push(data);
                });
            }
            else if (typeof(src) == "object") {
                target.push(src);
            }
            //TODO can't handle the function object
        };

        // ---------
        // merge the page data
        // ---------
        exports.mergePageData = function () {
            var size = arguments.length;
            if (size <= 0) {
                return {};
            } else if (size == 1) {
                return arguments[0];
            }
            var newObject = {};
            var views = [];
            $.each(arguments, function (i, object) {
                //direct override the views property.
                if (hasPro(newObject, 'views')) {
                    views = newObject['views'];
                }

                if (typeof(views) != 'undefined' && hasPro(object, 'views')) {
                    views = exports.arrayMerge(views, object['views'])
                }
                newObject = $.extend(newObject, object);
                newObject['views'] = views;
            });
            return newObject;
        };

        function hasPro(Object, proName) {
            if (Object[proName] == undefined) {
                console.log('should include [' + proName + '] property.');
                console.log(Object);
                return false;
            }
            return true;
        }

        function madeInnerHtml(elements) {
            if ($.isArray(elements)) {
                var elementHtmlArray = [];
                $.each(elements, function (i, element) {
                    var toInnerHtml = "";
                    if ($.isArray(element)) {
                        toInnerHtml = madeInnerHtml(element);
                    }
                    else {
                        toInnerHtml = madeTag(element);
                    }
                    elementHtmlArray[i] = toInnerHtml;
                });
                return elementHtmlArray;
            }

            return madeTag(elements);
        }

        function madeTag(element) {
            var hasTag = true;
            var tag = "";
            if (!hasPro(element, 'tag')) {
                hasTag = false;
            }
            else {
                tag = element['tag'];
            }
            var attr = {};
            if (hasPro(element, 'attr')) {
                attr = element['attr'];
            }
            var toInnerHtml = '';
            if (hasPro(element, 'content')) {
                toInnerHtml = element['content'];
            }
            else if (hasPro(element, 'sub')) {
                toInnerHtml = madeInnerHtml(element['sub']);
            }
            if (hasTag) {

                if ($.type(toInnerHtml) == 'string') {
                    if (toInnerHtml.indexOf('<') >= 0) {
                        var element = $.element[tag]();
                        element.attr(attr).append(toInnerHtml);
                        toInnerHtml = element;
                    } else {
                        toInnerHtml = $.element[tag](toInnerHtml).attr(attr);
                    }

                } else if ($.isArray(toInnerHtml)) {
                    var parent = $.element[tag]().attr(attr);
                    $.each(toInnerHtml, function (i, data) {
                        $(data).appendTo(parent);
                    });
                    toInnerHtml = parent;
                }
                else if ($.type(toInnerHtml) == 'object') {
                    toInnerHtml = $.element[tag](toInnerHtml).attr(attr);
                }
            }
            return toInnerHtml;
        }

    }
);